export default {
  namespaced: true,
  state: () => ({
    taskList: [
      { id: 100, name: '吃饭', isDone: true },
      { id: 201, name: '睡觉', isDone: false },
      { id: 103, name: '打豆豆', isDone: true }
    ],
    isWhat: '全部'
  }),
  mutations: {
    undateTaskIsDone (state, val) {
      const index = state.taskList.findIndex(task => task.id === val.id)
      state.taskList[index].isDone = val.value
    },
    delTask (state, val) {
      const index = state.taskList.findIndex(task => task.id === val)
      console.log(index)
      state.taskList.splice(index, 1)
    },
    addTask (state, val) {
      if (val === '') return alert('输入不能为空')
      const id = state.taskList.length !== 0 ? state.taskList[state.taskList.length - 1].id + 1 : 100
      state.taskList.push({
        id,
        name: val,
        isDone: false
      })
    },
    clearTask (state) {
      state.taskList = state.taskList.filter(item => !item.isDone)
    },
    updateIsWhat (state, val) {
      state.isWhat = val
    },
    updateIsDone (state, val) {
      state.taskList.forEach(item => { item.isDone = val })
    }

  },
  getters: {
    showList (state) {
      let arr = []
      if (state.isWhat === '未完成') {
        arr = state.taskList.filter(item => !item.isDone)
      } else if (state.isWhat === '已完成') {
        arr = state.taskList.filter(item => item.isDone)
      } else {
        arr = state.taskList
      }
      return arr
    }
  }
}
